package org.atguigu.gmall.realtime.function;

import com.alibaba.fastjson.JSONObject;
import org.apache.flink.api.common.functions.RichMapFunction;
import org.apache.flink.configuration.Configuration;
import org.atguigu.gmall.realtime.util.DimUtil;
import org.atguigu.gmall.realtime.util.JdbcUtil;
import org.atguigu.gmall.realtime.util.RedisUtil;
import redis.clients.jedis.Jedis;

import java.sql.Connection;

/**
 * Project:gmallRealTime
 * Package:org.atguigu.gmall.realtime.function
 * Author: ZengHaiFeng
 * CreateTime:2023/5/4 20:57:39
 * Description: Todo
 * Version：1.0
 */
public abstract class DimMapFunction<T> extends RichMapFunction<T,T> {
    private Connection conn;
    private Jedis redisClient;

    @Override
    public void open(Configuration parameters) throws Exception {
        conn = JdbcUtil.getPhoenixConnection();
        redisClient = RedisUtil.getRedisClient();
    }

    @Override
    public void close() throws Exception {
        JdbcUtil.closeConnection(conn);
        redisClient.close();
    }

    public abstract String getTable();
    public abstract String getId(T bean);
    public abstract void addDim(T bean, JSONObject dim);


    @Override
    public T map(T bean) throws Exception {
        JSONObject dim = DimUtil.readDim(redisClient,conn,getTable(),getId(bean));
        addDim(bean,dim);
        return bean;
    }
}
